﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FXSStation.Modules.SharedInfrastructureModule.BusinessClasses.Accounts;
using System.Collections.ObjectModel;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace FXSStation.Modules.SharedInfrastructureModule.BusinessClasses.Reports
{
    public class ComparisonReport : ReportBase
    {
        #region SecondaryAccount (INotifyPropertyChanged Property)
        private Account _secondaryAccount;

        public Account SecondaryAccount
        {
            get { return _secondaryAccount; }
            set
            {
                _secondaryAccount = value;
                RaisePropertyChanged("SecondaryAccount");
            }
        }
        #endregion

        #region PrimaryReports (INotifyPropertyChanged Property)
        private ObservableCollection<Report> _primaryReports;

        public ObservableCollection<Report> PrimaryReports
        {
            get { return _primaryReports; }
            set
            {
                _primaryReports = value;
                RaisePropertyChanged("PrimaryReports");
            }
        }
        #endregion

        #region SecondaryReports (INotifyPropertyChanged Property)
        private ObservableCollection<Report> _secondaryReports;

        public ObservableCollection<Report> SecondaryReports
        {
            get { return _secondaryReports; }
            set
            {
                _secondaryReports = value;
                RaisePropertyChanged("SecondaryReports");
            }
        }
        #endregion

        #region AvailableAccounts (INotifyPropertyChanged Property)
        private ObservableCollection<Account> _availableAccounts;

        public ObservableCollection<Account> AvailableAccounts
        {
            get { return _availableAccounts; }
            set
            {
                _availableAccounts = value;
                RaisePropertyChanged("AvailableAccounts");
            }
        }
        #endregion

        public ComparisonReport()
        {
            this.Type = ReportType.Comparison;
            DeSerializeAccounts();
        }
        public ComparisonReport(Account Account1, Account Account2)
        {
            this.Account = Account1;
            this.SecondaryAccount = Account2;
            this.Type = ReportType.Comparison;
            DeSerializeAccounts();
        }

        public override void RaisePropertyChanged(string propertyName)
        {
            base.RaisePropertyChanged(propertyName);
            if (propertyName == "Account" && this.Account != null)
            {
                this.PrimaryReports = Report.CreateFullReportFor(this.Account);
            }

            if (propertyName == "SecondaryAccount" && this.SecondaryAccount != null)
            {
                this.SecondaryReports = Report.CreateFullReportFor(this.SecondaryAccount);
            }
        }

        private void DeSerializeAccounts()
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileInfo file = new FileInfo(@"..\..\..\Modules\Accounts.bin");

            if (File.Exists(file.FullName))
            {
                FileStream fs = new FileStream(file.FullName, FileMode.Open);
                using (fs)
                {
                    this.AvailableAccounts = (ObservableCollection<Account>)formatter.Deserialize(fs);
                }
            }
        }
    }
}
